#! /usr/bin/env python
# -*- coding: utf-8 -*-
# __author__ = "life"
# Email: batistuta1977@163.com
# Date: 2017/7/11

import socket
ip_list = []   # 用于保存读取文件中的IP地址部分，类型列表
with open('host-ip.txt','r') as f1:
    for i in f1.readlines():
        sp = i.split('->')[1]
        # print(i.split('->')[1])
        ip_list.append(sp.strip())
# print(ip_list)
with open('ip-host.txt','w') as f2:
    for i in ip_list:
        print(i)
        if i == 'nofound':
            continue
        else:
            try:
                host = socket.gethostbyaddr(i)[0]  # 只取主机名部分
                f2.write(i + '->' + str(host) + '\n')
            except socket.herror as e:
                f2.write(i + '->' + 'nofound' + '\n')

# try:
#     result=socket.gethostbyaddr(sys.argv[1])
#     print('hostname is' + result[0])
# except socket.herror as e:
#     print('cannot lookup')